<html>
<head>
	<title>Excersise 1</title>
	<!--<link href="styles/js-console.css" rel="stylesheet" /> -->
</head>
<body>
	<div id="js-console"></div>
	<!--<script src="scripts/js-console.js"></script> -->
	<script>

function Solve(args) {
var k;
var input = new Array();

for(k=0;k<args.length;k++)
{
	input[k] = args[k].split(" ");
}

var i, j;
var counter = 1;
var numberN = parseInt(input[0][0]);
var numberM = parseInt(input[0][1]);
var jumps = parseInt(input[0][2]);
var posR = parseInt(input[1][0]);
var posC = parseInt(input[1][1]);
var temp = args.length-jumps;
var jumpSeq = new Array(jumps)

for(i=0;i<jumps;i++)
{
	jumpSeq[i] = new Array(2);
}
for(j=0;j<jumps;j++)
{
	for(k=0;k<2;k++)
	{
		jumpSeq[j][k] = parseInt(input[temp][k]);
	}
	temp++;
}

var field = new Array(numberN-1);
var mask = new Array(numberN-1);
for(i=0;i<=numberN-1;i++)
{
	field[i] = new Array(numberM-1);
	mask[i] = new Array(numberM-1);
}



for(i=0;i<numberN;i++)
{
	for(j=0;j<numberM;j++)
	{
		field[i][j] = counter;
		mask[i][j] = 0;
		counter++;
	}
}
var sum=field[posR][posC];
var escape = 0;
var cought = 0;
steps = 0;
do
{
for(i=0;i<jumps;i++)
{
	if((escape!=1))
	{
		if((posR+(jumpSeq[i][0])<=numberN)||(posC+(jumpSeq[i][1]))<=numberM)
		{
			escape = 1;
		}
		}
	//else
	//{
		if ((mask[posR+(jumpSeq[i][0])][posC+(jumpSeq[i][1])] != 1))
		{	
			sum = sum + field[posR+(jumpSeq[i][0])][posC+(jumpSeq[i][1])];
			mask[posR+(jumpSeq[i][0])][posC+(jumpSeq[i][1])] = 1;
			posR = posR+(jumpSeq[i][0]);
			posC = posC+(jumpSeq[i][1]);
			steps++;

		}
		else
		{
			cought = 1;
		}
	//}
	//}
}

}while((escape!=1)&&(cought!=1));
var answer;
if(cought==1)
{
answer = ("caught " + steps);
}
else
{
answer = ("escaped " + sum);
}
return console.log(answer);
}
var args = new Array("6 7 3","0 0","2 2","-2 2","3 -1");
Solve(args);
	</script>
</body>
</html>
